import { request, uploadFile } from '../utils/request';
import config from '../config/config';

// 用户相关接口
export const userApi = {
  // 手机号登录
  login: (data) => request({
    url: '/user/login',
    method: 'POST',
    data
  }),

  // 提交用户资料
  submitProfile: (data) => request({
    url: '/user/profile',
    method: 'POST',
    data
  }),

  // 获取用户资料
  getProfile: () => request({
    url: '/user/profile',
    method: 'GET'
  })
};

// 匹配相关接口
export const matchApi = {
  // 随机获取异性资料
  getRandom: () => request({
    url: '/match/random',
    method: 'GET'
  }).then(res => {
    // 确保返回的数据存在
    if (!res) return null;
    
    // 确保头像URL是完整路径
    if (res.userInfo?.avatarUrl && !res.userInfo.avatarUrl.startsWith('http')) {
      res.userInfo.avatarUrl = `${config.BASE_API}${res.userInfo.avatarUrl}`;
    }
    
    // 确保照片URL是完整路径
    if (res.photos) {
      res.photos = res.photos.map(url => 
        url.startsWith('http') ? url : `${config.BASE_API}${url}`
      );
    }
    
    return res;
  }),

  // 收藏/取消收藏
  toggleLike: (data) => request({
    url: '/match/like',
    method: 'POST',
    data
  }),

  // 获取收藏列表
  getLikes: (params) => request({
    url: '/match/likes',
    method: 'GET',
    data: params
  }),

  // 获取匹配历史
  getHistory: (params) => request({
    url: '/match/history',
    method: 'GET',
    data: params
  })
};

// 上传相关接口
export const uploadApi = {
  // 上传图片
  uploadImage: (filePath) => uploadFile({
    url: '/upload/image',
    filePath,
    name: 'file',
    formData: {
      type: 'profile'
    }
  }).then(res => {
    if (res.url && !res.url.startsWith('http')) {
      return {
        ...res,
        url: `${config.BASE_API}${res.url}`
      };
    }
    return res;
  })
}; 